const Service = require('egg').Service;
const { Sequelize, Op } = require('sequelize');

class StufileService extends Service {
    // 添加用service
    async add(pfile) {
        const { ctx } = this;
        const result = await ctx.model.Pubfile.create(pfile);
        return result;
    }
    // 分页查询用service
    async findList(keywords, pageIndex, pageSize) {
        const { ctx } = this;
        const result = await ctx.model.Pubfile.findAndCountAll({
            where: {
                filename: {
                    [Op.like]: '%' + keywords + '%'
                }
            },
            offset: (pageIndex - 1) * pageSize,
            limit: pageSize
        });
        return result;
    }
    // 根据id查找对应的值
    async findById(id) {
        const { ctx } = this;
        const result = await ctx.model.Pubfile.findByPk(id)
        return result;
    }
    // 删除
    async remove(id) {
        const { ctx } = this;
        const result = await ctx.model.Pubfile.destroy({
            where: { id }
        });
        return result;
    }
}
module.exports = StufileService;
